<template>
  <div class="search-form-container">
    <div class="search-bar">
      <el-input v-model="searchText" placeholder="请输入搜索车辆Id" />
      <el-button type="primary" @click="showForm">搜索</el-button>
    </div>

    <div class="form-container" v-if="showFormFlag">
      <el-form
        :model="formData"
        :rules="rules"
        ref="formRef"
        label-width="100px"
      >
        <el-form-item label="车辆ID" prop="order_id">
          <el-input v-model="formData.order_id" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formData.status" placeholder="请选择状态">
            <el-option label="可租赁" value="可租赁"></el-option>
            <el-option label="已预订" value="已预订"></el-option>
            <el-option label="使用中" value="使用中"></el-option>
            <el-option label="维修中" value="维修中"></el-option>
            <el-option label="已下线" value="已下线"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="location">
          <el-input v-model="formData.location" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">修改</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script setup>
import { ref } from "vue";
import carsApi from "@/http/cars.js";
import { userData } from "@/store/User";
import { messageSuccess, messageError } from "@/action/MessagePrompt";

const searchText = ref("");
const showFormFlag = ref(false);
const formData = ref({
  order_id: "",
  status: "",
  location: "",
});

const formRef = ref(null);
const userDataStore = userData();
const token = userDataStore.userInfo.token;

const rules = {
  order_id: [{ required: true, message: "车辆ID不能为空", trigger: "blur" }],
  status: [{ required: true, message: "状态不能为空", trigger: "change" }],
  location: [{ required: true, message: "地址不能为空", trigger: "blur" }],
};

const showForm = () => {
  let id = searchText.value;
  let params = [token, id];
  carsApi.getData(params).then((res) => {
    if (res.status === 0) {
      formData.value.order_id = res.data.order_id;
      formData.value.status = res.data.status;
      formData.value.location = res.data.location;
      showFormFlag.value = true;
      messageSuccess(res.message);
    } else {
      messageError(res.message);
    }
  });
};

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    let carData = {
      status: formData.value.status,
      location: formData.value.location,
    };
    let params = [token, formData.value.order_id, carData];
    if (valid) {
      carsApi.editData(params).then((res) => {
        if (res.status === 0) {
          messageSuccess(res.message);
          showFormFlag.value = false;
        } else {
          messageError(res.message);
        }
      });
    } else {
      // 表单验证失败
      return false;
    }
  });
};

const handleReset = () => {
  formData.value.order_id = "";
  formData.value.status = "";
  formData.value.location = "";
};
</script>
  
<style scoped>
.search-form-container {
  padding: 20px;
}

.search-bar {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.search-bar .el-input {
  margin-right: 10px;
  width: 300px;
}

.form-container {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 20px;
}
</style>